package View;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import Enums.ABMAction;


public class MainView implements ActionListener 
{	
	enum menuItemsAction {GENERAR_SIEMBRA_ACTION, REGISTRAR_COSECHA_ACTION, 
		AGREGAR_CEREAL_ACTION, MODIFICAR_CEREAL_ACTION, ELIMINAR_CEREAL_ACTION,
		AGREGAR_LOTE_ACTION, MODIFICAR_LOTE_ACTION, ELIMINAR_LOTE_ACTION,
		AGREGAR_MINERAL_ACTION, MODIFICAR_MINERAL_ACTION, ELIMINAR_MINERAL_ACTION};
	
	private JFrame frame;
	JInternalFrame internalFrame;
	private Hashtable<String, String> viewNames;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainView window = new MainView();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() 
	{
		viewNames = new Hashtable<String, String>();
		viewNames.put("GENERAR_SIEMBRA_ACTION", "Generar Siembra");
		viewNames.put("REGISTRAR_COSECHA_ACTION", "Registrar Cosecha");
		viewNames.put("REGISTRAR_COSECHA_ACTION", "Registrar Cosecha");
		viewNames.put("AGREGAR_CEREAL_ACTION", "Agregar Cereal");
		viewNames.put("MODIFICAR_CEREAL_ACTION", "Modificar Cereal");
		viewNames.put("ELIMINAR_CEREAL_ACTION", "Eliminar Cereal");
		viewNames.put("AGREGAR_LOTE_ACTION", "Agregar Lote");
		viewNames.put("MODIFICAR_LOTE_ACTION", "Modificar Lote");
		viewNames.put("ELIMINAR_LOTE_ACTION", "Eliminar Lote");
		viewNames.put("AGREGAR_MINERAL_ACTION", "Agregar Mineral");
		viewNames.put("MODIFICAR_MINERAL_ACTION", "Modificar Mineral");
		viewNames.put("ELIMINAR_MINERAL_ACTION", "Eliminar Mineral");
		
		frame = new JFrame();
		frame.setBounds(100, 100, 800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnSiembra = new JMenu("Siembra");
		menuBar.add(mnSiembra);
		
		JMenuItem mntmGenerarSiembra = new JMenuItem();
		mntmGenerarSiembra.setActionCommand(menuItemsAction.GENERAR_SIEMBRA_ACTION.toString());
		mntmGenerarSiembra.setText(viewNames.get((menuItemsAction.GENERAR_SIEMBRA_ACTION.toString())));
		mnSiembra.add(mntmGenerarSiembra);
		mntmGenerarSiembra.addActionListener(this);
		
		JMenu mnCosecha = new JMenu("Cosecha");
		menuBar.add(mnCosecha);
		
		JMenuItem mntmRegistrarCosecha = new JMenuItem();
		mntmRegistrarCosecha.setActionCommand(menuItemsAction.REGISTRAR_COSECHA_ACTION.toString());
		mntmRegistrarCosecha.setText(viewNames.get((menuItemsAction.REGISTRAR_COSECHA_ACTION.toString())));
		mnCosecha.add(mntmRegistrarCosecha);
		mntmRegistrarCosecha.addActionListener(this);
		
		JMenu mnCereal = new JMenu("Cereal");
		menuBar.add(mnCereal);
		
		JMenuItem mntmCrearCereal = new JMenuItem();
		mntmCrearCereal.setActionCommand(menuItemsAction.AGREGAR_CEREAL_ACTION.toString());
		mntmCrearCereal.setText(viewNames.get((menuItemsAction.AGREGAR_CEREAL_ACTION.toString())));
		mnCereal.add(mntmCrearCereal);
		mntmCrearCereal.addActionListener(this);
		
		JMenuItem mntmModificarCereal = new JMenuItem();
		mntmModificarCereal.setActionCommand(menuItemsAction.MODIFICAR_CEREAL_ACTION.toString());
		mntmModificarCereal.setText(viewNames.get((menuItemsAction.MODIFICAR_CEREAL_ACTION.toString())));
		mnCereal.add(mntmModificarCereal);
		mntmModificarCereal.addActionListener(this);
		
		JMenuItem mntmEliminarCereal = new JMenuItem();
		mntmEliminarCereal.setActionCommand(menuItemsAction.ELIMINAR_CEREAL_ACTION.toString());
		mntmEliminarCereal.setText(viewNames.get((menuItemsAction.ELIMINAR_CEREAL_ACTION.toString())));
		mnCereal.add(mntmEliminarCereal);
		mntmEliminarCereal.addActionListener(this);
		
		JMenu mnLote = new JMenu("Lote");
		menuBar.add(mnLote);
		mntmGenerarSiembra.addActionListener(this);
		
		JMenuItem mntmCrearLote = new JMenuItem();
		mntmCrearLote.setActionCommand(menuItemsAction.AGREGAR_LOTE_ACTION.toString());
		mntmCrearLote.setText(viewNames.get((menuItemsAction.AGREGAR_LOTE_ACTION.toString())));
		mnLote.add(mntmCrearLote);
		mntmCrearLote.addActionListener(this);
		
		JMenuItem mntmModificarLote = new JMenuItem();
		mntmModificarLote.setActionCommand(menuItemsAction.MODIFICAR_LOTE_ACTION.toString());
		mntmModificarLote.setText(viewNames.get((menuItemsAction.MODIFICAR_LOTE_ACTION.toString())));
		mnLote.add(mntmModificarLote);
		mntmModificarLote.addActionListener(this);
		
		JMenuItem mntmEliminarLote = new JMenuItem();
		mntmEliminarLote.setActionCommand(menuItemsAction.ELIMINAR_LOTE_ACTION.toString());
		mntmEliminarLote.setText(viewNames.get((menuItemsAction.ELIMINAR_LOTE_ACTION.toString())));
		mnLote.add(mntmEliminarLote);
		mntmEliminarLote.addActionListener(this);
		
		JMenu mnMineral = new JMenu("Mineral");
		menuBar.add(mnMineral);
		
		JMenuItem mntmCrearMineral = new JMenuItem();
		mntmCrearMineral.setActionCommand(menuItemsAction.AGREGAR_MINERAL_ACTION.toString());
		mntmCrearMineral.setText(viewNames.get((menuItemsAction.AGREGAR_MINERAL_ACTION.toString())));
		mnMineral.add(mntmCrearMineral);
		mntmCrearMineral.addActionListener(this);
		
		JMenuItem mntmModificarMineral = new JMenuItem();
		mntmModificarMineral.setActionCommand(menuItemsAction.MODIFICAR_MINERAL_ACTION.toString());
		mntmModificarMineral.setText(viewNames.get((menuItemsAction.MODIFICAR_MINERAL_ACTION.toString())));
		mnMineral.add(mntmModificarMineral);
		mntmModificarMineral.addActionListener(this);
		
		JMenuItem mntmEliminarMineral = new JMenuItem();
		mntmEliminarMineral.setActionCommand(menuItemsAction.ELIMINAR_MINERAL_ACTION.toString());
		mntmEliminarMineral.setText(viewNames.get((menuItemsAction.ELIMINAR_MINERAL_ACTION.toString())));
		mnMineral.add(mntmEliminarMineral);
		mntmEliminarMineral.addActionListener(this);
		
		frame.getContentPane().setLayout(new CardLayout(0, 0));
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if (internalFrame != null)
		{
			//Quito el internal frame anterior (si existe)
			frame.getContentPane().remove(internalFrame);
		}
		
		menuItemsAction action = menuItemsAction.valueOf(e.getActionCommand());
		String viewName = viewNames.get(e.getActionCommand());
		
		switch (action) 
		{
		case GENERAR_SIEMBRA_ACTION:
			internalFrame = new GenerarSiembraView(viewName);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case REGISTRAR_COSECHA_ACTION:
			internalFrame = new RegistrarCosechaView(viewName);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case AGREGAR_CEREAL_ACTION:
			internalFrame = new ABMCerealView(viewName, ABMAction.Alta);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case MODIFICAR_CEREAL_ACTION:
			internalFrame = new ABMCerealView(viewName, ABMAction.Modificacion);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case ELIMINAR_CEREAL_ACTION:
			internalFrame = new ABMCerealView(viewName, ABMAction.Baja);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case AGREGAR_LOTE_ACTION:
			internalFrame = new ABMLoteView(viewName,ABMAction.Alta);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case MODIFICAR_LOTE_ACTION:
			internalFrame = new ABMLoteView(viewName, ABMAction.Modificacion);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case ELIMINAR_LOTE_ACTION:
			internalFrame = new ABMLoteView(viewName, ABMAction.Baja);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case AGREGAR_MINERAL_ACTION:
			internalFrame = new ABMMineralView(viewName, ABMAction.Alta);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case MODIFICAR_MINERAL_ACTION:
			internalFrame = new ABMMineralView(viewName, ABMAction.Modificacion);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;
		case ELIMINAR_MINERAL_ACTION:
			internalFrame = new ABMMineralView(viewName, ABMAction.Baja);
			frame.getContentPane().add(internalFrame, "mainInternalFrame");
			break;

		default:
			break;
		}

    }
}
